"""
@name : 03.字典
@author : wenyao
@projectname: 空气质量.py
"""
#字典
#键值映射的数据结构
#字典是无序
#字典是可变数据类型
#key必须是唯一的， 天生去重

#字典的实现原理
#1、怎么查找的
#2、为什么key要是可hash对象
#3、为什么key天生去重

#字典的定义
# >>> a = {}
# >>> type(a)
# <class 'dict'>
# >>> b = dict()
# >>> type(b)
# <class 'dict'>
# >>> a = {"x":1,"y":2}
# >>> a = {"邓紫棋":"泡沫","梁静茹":"勇气"}
# >>> dict("abc")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# ValueError: dictionary update sequence element #0 has length 1; 2 is required
# >>> users = [("root","123456"),("admin","admin")]
# >>> dict(users)
# {'root': '123456', 'admin': 'admin'}


#####字典是无序的
#python3.6之后，字典的存储做了优化
# [root@fengdeyong ~]# python3
# Python 3.6.8 (default, Apr 16 2020, 01:36:27)
# [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
# Type "help", "copyright", "credits" or "license" for more information.
# >>> a = {"x":1,"u":0.5,"w":2}
# >>> a
# {'x': 1, 'u': 0.5, 'w': 2}
# >>> a = {"x":1,"b":0.5,"w":2,"c":2}
# >>> a
# {'x': 1, 'b': 0.5, 'w': 2, 'c': 2}
# >>>
# ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# Type "help", "copyright", "credits" or "license" for more information.
# >>> a = {"x":1,"u":0.5,"w":2}
# >>> a
# {'x': 1, 'u': 0.5, 'w': 2}
# >>> a = {"x":1,"b":0.5,"w":2}
# >>> a
# {'x': 1, 'b': 0.5, 'w': 2}
# >>> a = {"x":1,"b":0.5,"w":2,"c":2}
# >>> a
# {'x': 1, 'c': 2, 'b': 0.5, 'w': 2}

#字典的定义：
#key必须是可hash对象（不可变数据类型）
#value可以是任意值
# >>> a = {[1]:1}
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: unhashable type: 'list'
# >>> a = {(1):1}

##天生去重
# >>> a = {"x":2,"x":3}
# >>> a
# {'x': 3}

#####新增、修改、取值########
# >>> a
# {'x': 3}
# >>> a['c'] = 4
# >>> a
# {'x': 3, 'c': 4}
# >>> a['c'] = 5
# >>> a
# {'x': 3, 'c': 5}
# >>> a['c']
# 5
# >>> a["d"]
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# KeyError: 'd'
# >>> a.get('d',0)
# 0
# >>> a.get('c',0)
# 5
# >>> a.get('d')
# >>> result = a.get('d')
# >>> print(result)
# None

#删除
#  字典基本操作-删除
# • 用法1：dicts.pop(key)
# • 用法2：dicts.popitem()
# >>> a
# {'x': 3, 'c': 5}
# >>> a.pop()
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: pop expected at least 1 arguments, got 0
# >>> a.pop("c")
# 5
# >>> a
# {'x': 3}
# >>> result = a.pop("x")
# >>> result
# 3
# >>> a
# {}
# >>> a = {"x":1,"y":2,"z":3}
# >>> a.popitem()
# ('z', 3)

######字典的合并
#用法1：dict1.update(dict2) => 将dict2合并到dict1
# >>> a
# {'y': 2}
# >>> b
# {'x': 3}
# >>> #a.update(b)
# ...
# >>> id(a)
# 140265103813080
# >>> a.update(b)
# >>> a
# {'y': 2, 'x': 3}
# >>> id(a)
# 140265103813080
# >>> a+b
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

#方式二： dict(dict1, **dict2) => 将dict1与dict2合并生成一个新字典
# >>> a
# {'y': 2, 'x': 3}
# >>> b
# {'z': 4}
# >>> dict(a,**b)
# {'y': 2, 'x': 3, 'z': 4}

#pack 和 unpack
#字典 dict1 =  {"a":1, "b":2}  , **dict1  --->a=1,b=2
# a = {"name":"sc", "age":18}
# print("my name is {name},age is {age}".format(name="sc", age=18))
# print("my name is {name},age is {age}".format(**a))
#
# t1 = (10,20)  # *t1  ---> 10,20
# print("num1 is {0}, num2 is {1}".format(10,20))
# print("num1 is {0}, num2 is {1}".format(*t1))


#成员关系运算符
# >>> a
# {'y': 2, 'x': 3}
# >>> "x" in a
# True
# >>> "3" in a
# False
# >>> 3 in a
# False
# >>> a in a.values()
# False
# >>> 3 in a.values()
# True
# >>> a.values
# <built-in method values of dict object at 0x7f9203afe9d8>
# >>> a.values()
# dict_values([2, 3])
# >>> a.keys()
# dict_keys(['y', 'x'])


#字典的遍历
# >>> for i in a:
# ...     print(i)
# ...
# y
# x
# >>> for i in enumerate(a):
# ...     print(i)
# ...
# (0, 'y')
# (1, 'x')
# >>> a
# {'y': 2, 'x': 3}
# >>> for i in a.items():
# ...    print(i)
# ...
# ('y', 2)
# ('x', 3)
# >>> for i,j in a.items():
# ...    print(i,j)
# ...
# y 2
# x 3


company={ 'BOSS':'唐僧','CTO':'悟空','CFO': '八戒','搬砖':'沙僧','苦力':'白龙马'}
print(company['CTO'],company.get("CTO"))
print(company['BOSS'])
#方法一
for i in company:
    print(f"{i}是{company[i]}")
#方法二
for i,j in company.items():
    print(f"{i}是{j}")

person1 = input("请输入'悟空':")
person2 = input("请输入'白骨精':")
print(f"'悟空'是否在company:{person1 in company.values()}")
print(person1, "在" if person1 in company.values() else "不在", "company中")
print(f"'白骨精'是否在company:{person2 in company.values()}")